package com.zzyl.controller;

import com.aliyun.iot20180120.models.DeleteDeviceRequest;
import com.aliyun.iot20180120.models.QueryDevicePropertyStatusRequest;
import com.aliyun.iot20180120.models.QueryThingModelPublishedRequest;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.DeviceDto;
import com.zzyl.service.DeviceService;
import com.zzyl.vo.DeviceVo;
import com.zzyl.vo.ProductVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.zzyl.base.ResponseResult.success;

@RestController
@RequestMapping("/iot")
@Api(tags = "管理端 - 物联网iot相关接口")
public class DeviceController {
    @Autowired
    private DeviceService deviceService;


    /**
     * 从物联网平台同步产品列表
     *
     * @return
     */
    @PostMapping("/syncProductList")
    @ApiOperation(value = "从物联网平台同步产品列表")
    public ResponseResult<Void> syncProductList() {
        deviceService.syncProductList();
        return success();
    }

    /**
     * 查询所有产品列表
     *
     * @return
     */
    @GetMapping("/allProduct")
    @ApiOperation(value = "查询所有产品列表")
    public ResponseResult<List<ProductVo>> allProduct() {

        return success(deviceService.allProduct());
    }


    /**
     * 注册设备
     *
     * @param deviceDto
     * @return
     */
    @PostMapping("/RegisterDevice")
    @ApiOperation(value = "注册设备")
    public ResponseResult<Void> registerDevice(@RequestBody DeviceDto deviceDto) {
        return deviceService.registerDevice(deviceDto);
    }

    /**
     * 分页查询设备列表
     */
    @GetMapping("/pageQueryDevice")
    @ApiOperation(value = "分页查询设备列表")
    public ResponseResult<DeviceVo> findByPage(Integer pageNum, Integer pageSize,
                                               String deviceName, Integer locationType,
                                               String productKey) {

        PageResponse<DeviceVo> pageInfo = deviceService.findByPage(pageNum, pageSize, deviceName, locationType, productKey);
        return success(pageInfo);

    }

    /**
     * 查询设备详细数据
     */
    @PostMapping("/QueryDeviceDetail")
    @ApiOperation(value = "查询设备详细数据")
    public ResponseResult<DeviceVo> findByDetails(@RequestBody DeviceDto deviceDto) {
        DeviceVo deviceVo = deviceService.findByDetails(deviceDto);
        return success(deviceVo);

    }

    /**
     * 查询指定设备的物模型运行状态
     */
    @PostMapping("/QueryDevicePropertyStatus")
    @ApiOperation(value = "查询指定设备的物模型运行状态")
    public ResponseResult queryDevicePropertyStatus(@RequestBody QueryDevicePropertyStatusRequest request) {
        ResponseResult result = deviceService.queryDevicePropertyStatus(request);
        return success(result);
    }


    /**
     * 查看指定产品的已发布物模型中的功能定义详情
     */
    @PostMapping("/QueryThingModelPublished")
    @ApiOperation(value = "查看指定产品的已发布物模型中的功能定义详情")
    public ResponseResult queryThingModelPublished(@RequestBody QueryThingModelPublishedRequest request) {

        ResponseResult result = deviceService.queryThingModelPublished(request);
        return success(result);
    }


    /**
     * 修改设备备注名称
     */
    @PostMapping("/UpdateDevice")
    @ApiOperation(value = "修改设备备注名称")
    public ResponseResult updateDeviceNotes(@RequestBody DeviceDto deviceDto) {
        deviceService.updateDeviceNotes(deviceDto);
        return success();
    }


    /**
     * 删除设备
     */
    @DeleteMapping("")
    @ApiOperation(value = "删除设备")
    public ResponseResult deleteByIotId(@RequestBody DeleteDeviceRequest request) {

        deviceService.deleteByIotId(request);

        return success();
    }


}